home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15937 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  80 lines

  1. Path: itssrv1.ucsf.edu!usenet
  2. From: Aneil Mallavarapu <mallav@itsa.ucsf.edu>
  3. Newsgroups: comp.lang.c++
  4. Subject: overriding non-virtual functions
  5. Date: Mon, 08 Apr 1996 12:10:49 -0700
  6. Organization: UCSF, ITS
  7. Message-ID: <316964B9.F50@itsa.ucsf.edu>
  8. NNTP-Posting-Host: mitchisonlab-pc1.ucsf.edu
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.01 (Win16; I)
  13.  
  14. I need to override a non-virtual function declared in a base class.
  15.  
  16. Some of the code is non-modifiable, and I have denoted it NM<name>.
  17.  
  18. Here's my problem:
  19.  
  20. 1) the base class (NMBaseClass) contains a function, NMfunc(). It is not 
  21. virtual.  
  22.  
  23. 2) Another function (NMCallIt) takes a pointer to the base class and uses it to 
  24. call NMfunc().
  25.  
  26. 3) I derive a new class (MyClass) from the base class.  
  27.  
  28. 4) I want to pass MyClass to NMCallIt, but I want NMCallIt to use my own 
  29. version of NMfunc().  Which I'll provide in the MyClass declaration.
  30.  
  31. Is this possible?  I have written this out in code to make it clearer:
  32.  
  33.  
  34. // I cannot modify this code:
  35.  
  36. class NMBaseClass    // non modifiable base class
  37. {
  38.     void NMfunc();    // not a virtual function! 
  39. }
  40. NMBaseClass::NMfunc()
  41. {
  42.     ...
  43. }
  44.  
  45. NMCallIt(NMBaseClass* pNMBC) // non modifiable function
  46. {
  47.     ...
  48.     pNMBC->NMfunc();    
  49.     ...
  50. }
  51.  
  52.  
  53. // I can change this code:
  54.  
  55. class MyClass : NMBaseClass
  56. {
  57.     void NMfunc();    // I want to use this to override
  58.             // NMBaseClass::NMfunc()
  59. }
  60.  
  61. void MyClass::NMfunc()
  62. {
  63.     // my code ...
  64. }
  65.  
  66.  
  67. main()
  68. {
  69.     MyClass    myobject;
  70.  
  71.     NMCallIt( (NMBaseClass*)&myobject );
  72.  
  73.         // NMCallIt calls NMBaseClass::NMfunc()
  74.         // But, I want it to call MyClass::NMfunc()
  75. }
  76.  
  77. Thanks for any help
  78.  
  79. Aneil Mallavarapu (mallav@itsa.ucsf.edu)
  80.